package com.miaomiao.handler;


import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.miaomiao.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;


@Component
public class MyMateObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        String username = null;
        try {
            username = SecurityUtils.getUsername();
        } catch (Exception e){
            e.printStackTrace();
            username = "regist";
        }
        LocalDateTime localDateTime = LocalDateTime.now();
        ZoneId zoneId = ZoneId.systemDefault();
        this.strictInsertFill(metaObject,"createTime", Date.class,Date.from(localDateTime.atZone(zoneId).toInstant()));
        this.strictInsertFill(metaObject,"createBy",String.class, username);
        this.strictInsertFill(metaObject,"updateTime", Date.class,Date.from(localDateTime.atZone(zoneId).toInstant()));
        this.strictInsertFill(metaObject,"updateBy", String.class,username);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        String username = null;
        try {
            username = SecurityUtils.getUsername();
        } catch (Exception e){
            e.printStackTrace();
            username = "regist";
        }
        LocalDateTime localDateTime = LocalDateTime.now();
        ZoneId zoneId = ZoneId.systemDefault();
        this.strictInsertFill(metaObject,"updateTime", Date.class,Date.from(localDateTime.atZone(zoneId).toInstant()));
        this.strictInsertFill(metaObject,"updateBy", String.class,username);
    }
}
